package globulin;

import java.io.*;
import javazoom.jl.player.Player;
import java.net.*;

/** @author Sean Pavlichek
 */
public class MusicPlayer {
    // Necessary variables
    private BufferedInputStream bis;
    private Player mp3Player;
    private URL[] songList;
    private MusicThread thread;
    private Globulin parent;

    /** Basic constructor. Initializes an empty MusicPlayer object.
     * @param inputParent The game. This is so that this class can access certain
     * methods from the game class.
     */
    public MusicPlayer(Globulin inputParent) {
        this.parent = inputParent;
        songList = new URL[]{parent.getClass().getResource("media/music/ghostsnstuff.mp3")};
    }

    /** Chooses a specific song from the built in list of songs.
     * @param index The index of the song to play.
     */
    public void selectSong(int index) {
        // If the index is valid
        if ((index < songList.length) && (index >= 0)) {
            // Starts the file reader
            try {
                bis = new BufferedInputStream(songList[index].openStream());
                // Runs the player
                playMusic(index);
            } catch (FileNotFoundException fnf) {
                System.out.println("fnf");
            } catch (IOException io) {
            }
        }
    }

     /** Chooses a random song from the built in list of songs.
     */
    public void selectRandomSong() {
        int index = parent.random(0,songList.length - 1,1);
        // If the index is valid
        if ((index < songList.length) && (index >= 0)) {
            // Starts the file reader
            try {
                bis = new BufferedInputStream(songList[index].openStream());
                // Runs the player
                playMusic(index);
            } catch (FileNotFoundException fnf) {
                System.out.println("fnf");
            } catch (IOException io) {
            }
        }
    }

     /** Chooses a random song from the built in list of songs, avoiding the
      * selected song. This is to prevent the same song from being played twice
      * in a row.
      * @param notThisIndex The song that is not to be played.
     */
    public void selectRandomSong(int notThisIndex) {
        int index = parent.random(0,songList.length - 1,1);
        while ((index == notThisIndex) && (songList.length != 1)) {
            index = parent.random(0,songList.length,1);
        }
        // If the index is valid
        if ((index < songList.length) && (index >= 0)) {
            // Starts the file reader
            try {
                bis = new BufferedInputStream(songList[index].openStream());
                // Runs the player
                playMusic(index);
            } catch (FileNotFoundException fnf) {
                System.out.println("fnf");
            } catch (IOException io) {
            }
        }
    }

    /** Starts the song that has been selected.
     */
    private void playMusic(int index) {
        try {
            // Starts the music in its own thread so that the program can still run
            mp3Player = new Player(bis);
            thread = new MusicThread(mp3Player, this, index);
            (new Thread(thread)).start();
        } catch (javazoom.jl.decoder.JavaLayerException jle) {
        }
    }
}
